package net.zjitc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.zjitc.mapper.AdminMapper;
import net.zjitc.pojo.Admin;
import net.zjitc.service.AdminService;
import net.zjitc.utils.JwtUtil;
import net.zjitc.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin login(Admin admin) throws NoSuchAlgorithmException {
        admin.setPassword(MD5Utils.hashPassword(admin.getPassword(),"ItCast"));
        return adminMapper.login(admin);
    }

    @Override
    public Admin getAdmin(String token) {
        String loginUsername = new JwtUtil().getUsernameFromToken(token);
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("username",loginUsername);
        return adminMapper.selectByName(loginUsername);
    }

    @Override
    public Integer updateAdmin(Admin admin) throws NoSuchAlgorithmException {
        admin.setPassword(MD5Utils.hashPassword(admin.getPassword(),"ItCast"));
        return adminMapper.updateById(admin);
    }
    @Override
    public Integer updateAvatar(Admin admin) {
        return adminMapper.updateAvatar(admin);
    }
}
